<template>
  <el-dialog :title="info.title" :visible.sync="info.isShow">
    <el-form :model="form">
      <el-form-item label="角色名称" :label-width="formLabelWidth">
        <el-input v-model="form.rolename" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="角色权限" :label-width="formLabelWidth">
        <el-tree
          :data="data"
          show-checkbox
          node-key="id"
          :default-checked-keys="defaultkeys"
          :props="defaultProps"
          ref="tree"
        ></el-tree>
      </el-form-item>
      <el-form-item label="状态" :label-width="formLabelWidth">
        <el-switch
          v-model="form.status"
          active-color="green"
          inactive-color="grey"
          :active-value="1"
          :inactive-value="2"
        ></el-switch>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button v-if="info.isAdd" type="primary" @click="comfirm">确 定</el-button>
      <el-button v-else type="primary" @click="change">修 改</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getMenu, addRole, oneRole, changeRole } from "../../../request/api";
import { successAlert } from "../../../utils/alert";
export default {
  props: ["info"],
  data() {
    return {
      form: {
        rolename: "",
        menus: "", //角色权限
        status: 1 //1正常  2禁用
      },
      formLabelWidth: "120px",
      defaultProps: {
        children: "children",
        label: "title"
      },
      data: [], //菜单列表权限
      defaultkeys: [] //默认选中
    };
  },
  methods: {
    requestMenu() {
      getMenu({ istree: true }).then(res => {
        if (res.code == 200) {
          // console.log(res.list);
          this.data = res.list;
        }
      });
    },
    comfirm() {
      //确定
      this.form.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
      addRole(this.form).then(res => {
        if (res.code === 200) {
          successAlert(res.msg);
          this.cancel();
          this.$eventBus.$emit("message");
        }
      });
    },
    cancel() {
      this.info.isShow = false;
      this.form = {
        rolename: "",
        menus: "", //角色权限
        status: 1 //1正常  2禁用
      };
      this.$refs.tree.setCheckedKeys([]); //因为是个组件,所以用ref
    },
    getdetail(id) {
      oneRole({ id }).then(res => {
        if (res.code === 200) {
          // console.log(res.list);
          this.defaultkeys = JSON.parse(res.list.menus);
          this.form = res.list;
          this.form.id = id;
        }
      });
    },
    change() {
        this.form.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
      changeRole(this.form).then(res => {
        if (res.code === 200) {
            // console.log(this.form);
          successAlert(res.msg);
          this.cancel();
          this.$eventBus.$emit("message");
        }
      });
    }
  },
  mounted() {
    this.requestMenu();
  }
};
</script>

<style>
</style>